#include "CylinderMesh.h"
#include "VertexList.h"
#include "TriangleList.h"
#include "ObjMesh.h"
#define _USE_MATH_DEFINES
#include "math.h"

CylinderMesh::CylinderMesh(float radius, float height, int nrOfSegments):m_Position(0,0,0),m_Radius(radius),m_Height(height),m_NrOfSegments(nrOfSegments)
{
	CreateMesh();
}


CylinderMesh::CylinderMesh(DAEFloat3 position, float radius, float height, int nrOfSegments):m_Position(position),m_Radius(radius),m_Height(height),m_NrOfSegments(nrOfSegments)
{
	CreateMesh();
}

void CylinderMesh::CreateMesh()
{
	// create vertex list with only positions.
	VertexList* pVertexList = VertexList::Create3DPosColorVL(m_NrOfSegments * 2);
	int index = -1;
	for ( int i = 0 ; i< m_NrOfSegments ; ++i ){
		float angle = (float)(2 * i * M_PI / m_NrOfSegments);
		float x = m_Radius * cos( angle );
		float y = m_Radius * sin( angle);

		DAEFloat3 bv = DAEFloat3(x,y,0 ) + m_Position;
		DAEFloat3 uv = DAEFloat3(x,y,m_Height) + m_Position;

		pVertexList->AddVertexData(++index, POSITION, &bv);
		pVertexList->AddVertexData(++index, POSITION, &uv);
	}
	DAEFloat3 color(1,1,1);
	pVertexList->FillSemantic(COLOR,&color);


	TriangleList* pTriangleList = new TriangleList( m_NrOfSegments* 2 );

	for ( int i = 0 ; i < m_NrOfSegments; ++i ){
		int index1 = i*2;
		int index2 = index1+1;
		
		int index3 = index2+1;
		int index4 = index3+1;
		if ( i == m_NrOfSegments -1 )
		{
			index3 = 0;
			index4 = 1;
		}
		pTriangleList->AddIndexedFace(index1, index2, index4);
		pTriangleList->AddIndexedFace(index1, index4, index3);
	}
	for ( int i = 0;  i < m_NrOfSegments*2; ++i )
	{
		pVertexList->GetVertexData(i,COLOR,&color);
	}
	
	m_pMesh = new ObjMesh(pVertexList,pTriangleList);
}

CylinderMesh::~CylinderMesh()
{
	delete m_pMesh;

}